<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Constants\ContactDedupTaskStatusConstants;
use App\CustHub\Model\ChContactDedupTask;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;
use Mine\MineModel;

class ChContactDedupTaskMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChContactDedupTask::class;
    }

    /**
     * 获取任务信息
     * @param mixed $id
     * @return MineModel|Model|string|Builder|null
     */
    public function findById(mixed $id): \Mine\MineModel|Model|string|Builder|null
    {
        return $this->model::query()->where('id',$id)->first();
    }

    /**
     * 检测任务是否存在
     * @param mixed $uid
     * @return bool
     */
    public function taskExists(mixed $uid): bool
    {
        return $this->model::query()
            ->where('uid',$uid)
            ->whereIn('status',[ContactDedupTaskStatusConstants::DETECTING,ContactDedupTaskStatusConstants::DETECTED])
            ->exists();
    }

    public function findByUid(mixed $uid): ChContactDedupTask|\Hyperf\Database\Query\Builder|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('uid',$uid)
            ->whereIn('status',[ContactDedupTaskStatusConstants::DETECTING,ContactDedupTaskStatusConstants::DETECTED])
            ->first();
    }

    /**
     * 更新状态
     * @param mixed $taskId
     * @param int $status
     * @return bool|int
     */
    public function updateStatus(mixed $taskId,int $status): bool|int
    {
        return $this->model::query()->where('id',$taskId)->update(['status'=>$status]);
    }
}